package com.im.app.controller;

import com.im.app.model.YunConfig;
import com.im.app.service.IYunConfigService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/yunConfig")
public class YunConfigController {

    @Resource
    private IYunConfigService iYunConfigService;


    @GetMapping(value = "/queryYunConfigList")
    public ResponseResult<Object> queryYunConfigList() {
        //调用业务层返回列表
        List<YunConfig> yunConfigList = iYunConfigService.selectYunConfigList(new YunConfig());
        Map<String,Object> map = new HashMap<>();
        for (YunConfig yunConfig : yunConfigList) {
            if("apiUrl".equals(yunConfig.getDictKey())){
                map.put("apiUrl",yunConfig.getDictValue());
            }else if("ioUrl".equals(yunConfig.getDictKey())){
                map.put("ioUrl",yunConfig.getDictValue());
            }else if("ioPort".equals(yunConfig.getDictKey())){
                map.put("ioPort",Integer.valueOf(yunConfig.getDictValue()));
            }else if("uploadUrl".equals(yunConfig.getDictKey())){
                map.put("uploadUrl",yunConfig.getDictValue());
            }else if("version".equals(yunConfig.getDictKey())){
                map.put("version",yunConfig.getDictValue());
            }else if("updateAddress".equals(yunConfig.getDictKey())){
                map.put("updateAddress",yunConfig.getDictValue());
            }else if("down".equals(yunConfig.getDictKey())){
                map.put("down",yunConfig.getDictValue());
            }
        }
        return Response.makeOKRsp(map);
    }



    @GetMapping(value ="/find")
    public ResponseResult<YunConfig> findYunConfig(Integer id) {
        try {
            //调用业务层返回列表
            YunConfig yunConfig = iYunConfigService.selectYunConfigById(Long.valueOf(id));
            return Response.makeOKRsp(yunConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询配置信息异常");
        }
    }

    @GetMapping(value ="/delete")
    public ResponseResult deleteYunConfig(Integer id) {
        int result = iYunConfigService.deleteYunConfigById(Long.valueOf(id));
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }
    }

    @PostMapping(value ="/add")
    public ResponseResult<Object> addYunConfig(@RequestBody YunConfig yunConfig) {
        int result = iYunConfigService.insertYunConfig(yunConfig);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @PostMapping(value ="/update")
    public ResponseResult updateYunConfig(@RequestBody YunConfig yunConfig) {
        int result = iYunConfigService.updateYunConfig(yunConfig);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }








}
